﻿$(function () {
    $('#txtActivityEndTime').bind("focus", function () {
        WdatePicker({ skin: 'whyGreen', minDate: '#F{$dp.$D(\'txtActivityStartTime\')}', isShowClear: true, readOnly: true });
    });
    $('#txtActivityStartTime').bind("focus", function () {
        var EndTime = $dp.$('txtActivityEndTime');
        WdatePicker({ skin: 'whyGreen', minDate: '%y-%M-%d', isShowClear: true, readOnly: true, onpicked: function () { EndTime.focus(); } });
    });

    //新增活动
    $("#btnActivityAdd").bind("click", btnActivityAddClick);

    //保存
    $("#btnActivitySave").bind("click", btnActivitySaveClick);
})

//新增营销活动
function btnActivityAddClick() {
    $("#txtActivityTitle,#txtActivityContents,#txtActivityStartTime,#txtActivityEndTime").val("");    
    $("#radCoverYes").attr("checked", true);

    artDialog({
        title: '新增活动',
        content: document.getElementById('ActivityInfo'),
        id: 'ActivityInfo',
        lock: true
    })
}

//查看活动奖励
function ShowAwards(ActivityID) {
    var height = $(window).height();
    var width = $(window).width();
    art.dialog.open("../Activity/ActivityAwards.aspx?ActivityID=" + ActivityID, { title: "查看活动奖励", lock: true, width: "30%", height: "30%" });

}


//保存
function btnActivitySaveClick() {
    var txtActivityTitle = $.trim($("#txtActivityTitle").val());
    var txtActivityContents = $.trim($("#txtActivityContents").val()); 
    var txtActivityStartTime = $.trim($("#txtActivityStartTime").val());
    var txtActivityEndTime = $.trim($("#txtActivityEndTime").val());
    var radCoverYes = $("#radCoverYes").attr("checked");
    var scoreID = $("#Score").val();
    var MoneyID = $("#Money").val();
    var RebateID = $("#Rebate").val();
    var MoneyRebateID = $("#MoneyRebate").val();
    var PresentID = $("#Present").val();
    var ExtendID = $("#Extend").val();
    var ShareID = $("#Share").val();
    
    var strErrorMsg = "";
    if (txtActivityTitle == "") { strErrorMsg += "<li>请填写活动标题</li>"; }
    if (txtActivityStartTime == "" || txtActivityEndTime == "") { strErrorMsg += "<li>请选择正确的活动日期</li>"; }

    if (strErrorMsg != "") {
        strErrorMsg = "<ul>" + strErrorMsg + "</ul>";
        art.dialog({
            title: '系统提醒',
            icon: 'error', //图标
            content: strErrorMsg,
            lock: true
        });
        return false;
    }

    art.dialog({
        title: '系统提示',
        content: "将新增营销活动。\n确定操作吗？",
        lock: true,
        ok: function () {
            doAjax("../", "ActivityAdd", {
                "txtActivityTitle": txtActivityTitle,
                "txtActivityContents": txtActivityContents,               
                "txtActivityStartTime": txtActivityStartTime,
                "txtActivityEndTime": txtActivityEndTime,
                "radCoverYes": radCoverYes ? 1 : 0,
                "scoreID": scoreID ? scoreID : "0",
                "MoneyID": MoneyID ? MoneyID : "0",
                "RebateID": RebateID ? RebateID : "0",
                "TicketID":  "0",
                "PresentID": PresentID ? PresentID : "0",
                "ExtendID": ExtendID ? ExtendID : "0",
                "ShareID": ShareID ? ShareID : "0",
                "MoneyRebateID": MoneyRebateID?MoneyRebateID:"0",
            }, "text", function (text) {
                if (text == "0") {
                    art.dialog({
                        title: '系统提示',
                        time: 4,
                        content: ("系统异常，未保存数据，请再次点击保存！"),
                        lock: true
                    });
                } else {
                    art.dialog({
                        title: '系统提示',
                        time: 0.5,
                        content: '保存成功！',
                        close: function () { window.location = window.location; },
                        lock: true
                    });
                }
            })
        },
        cancelVal: '取消',
        cancel: true
    })
}



//修改活动状态
function btnPromotionsSwitch(ActivityID, ActivityState) {
    var state = ActivityState == 1 ? "开启" : "关闭";
    var oppsite_state = ActivityState == 0 ? "开启" : "关闭";
    art.dialog({
        title: "系统提示",
        lock: true,
        content: '该活动现在是【' + state + '】状态，确定修改为【' + oppsite_state + '】吗? ',
        ok: function () {
            this.lock();
            doAjax("../", "ActivityChangeStatus", {
                "ActivityID": ActivityID,
                "ActivityState": ActivityState
            }, "text", function (text) {
                if (text == "0") {
                    art.dialog({
                        title: '系统提示',
                        time: 4,
                        content: ("系统异常，未保存数据，请再次点击保存！"),
                        lock: true
                    });
                } else {
                    art.dialog({
                        title: '系统提示',
                        time: 0.5,
                        content: '修改成功！',
                        close: function () { window.location = window.location; },
                        lock: true
                    });
                }
            })
            return false;
        },
        cancelVal: '取消',
        cancel: true //为true等价于function(){}
    })
}

